<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Verifycode extends Model {
	// 生成验证码
	function getCode($map) {
		$info = $this->getInfo ( $map );
		$code = mt_rand ( 01010, 98979 );
		if (count ( $info ) > 0) {
			if (date ( 'Ymd' ) == date ( 'Ymd', $info ['create_time'] )) {
				$data ['send_total'] = $info ['send_total'] + 1;
				$data ['code'] = $code;
				$log = $this->edit ( $map, $data );
			} else {
				$data ['send_total'] = 1;
				$data ['code'] = $code;
				$log = $this->edit ( $map, $data );
			}
		} else {
			$data ['account'] = $map ['account'];
			$data ['type'] = $map ['type'];
			$data ['create_time'] = time ();
			$data ['send_total'] = 1;
			$data ['code'] = $code;
			$log = $this->add ( $data );
		}
		if ($log > 0)
			return $code;
		else
			return '';
	}
	function getInfo($map) {
		return db ( 'verifycode' )->where ( $map )->find ();
	}
	function add($data) {
		return db ( 'verifycode' )->insert ( $data );
	}
	function edit($map, $data) {
		return db ( 'verifycode' )->where ( $map )->update ( $data );
	}
}

?>